from selenium import webdriver
import time as tm
from bs4 import BeautifulSoup
from selenium.webdriver.chrome.options import Options

Url = "http://221.233.24.23/eams/login.action"
InfoUrl = "http://221.233.24.23/eams/stdDetail.action"
GradeUrl = "http://221.233.24.23/eams/teach/grade/course/person!historyCourseGrade.action?projectType=MAJOR"

def getInfos(user_id,password):
    driver = webdriver.PhantomJS()
    driver.get(Url)
    username = driver.find_element_by_id("username")
    passwd = driver.find_element_by_id("password")
    submit = driver.find_element_by_name("submitBtn")
    username.send_keys(user_id)
    passwd.send_keys(password)
    tm.sleep(1)
    submit.click()
    driver.get(InfoUrl)
    html = driver.page_source
    soup = BeautifulSoup(html, "lxml")
    infos = {}
    keys = []
    vals = []
    trs = soup.find_all("tr")
    for tr in trs[1:-1]:
        tds = tr.find_all("td")
        if len(tds) == 0:
            continue
        key1 = tds[0].getText()[:-1]
        val1 = tds[1].getText()
        key2 = tds[2].getText()[:-1]
        val2 = tds[3].getText()
        keys.append(key1)
        keys.append(key2)
        vals.append(val1)
        vals.append(val2)
    for i in range(len(vals)-1):
        infos[keys[i]] = vals[i]
    print(infos)
    
    driver.get(GradeUrl)
    html = driver.page_source
    soup = BeautifulSoup(html,"lxml")
    tables = soup.find_all("table")
    point_trs = tables[0].find_all("tr")
    grades_trs = tables[1].find_all("tr")
    
    print(point_trs)
    print(grades_trs)
    
    
    time = "2"+point_trs[-1].getText().split("2")[-1]
    all_point_tds = point_trs[-2].find_all("th")
    all_point = {}
    all_point_keys = ["种类","必修门数","必修总学分","必修平均绩点"]
    point_keys = []
    grade_keys = []
    points = []
    grades = []
    for idx, all_point_td in enumerate(all_point_tds):
        all_point[all_point_keys[idx]] = all_point_td.getText()
    for point_th in point_trs[0].find_all("th"):
        point_keys.append(point_th.getText())
    for grade_th in grades_trs[0].find_all("th"):
        grade_keys.append(grade_th.getText())
    print("-----"*20)
    print(all_point)
    for point_tr in point_trs[1:-2]:
        point = {}
        point_tds = point_tr.find_all("td")
        for idx, point_td in enumerate(point_tds):
            point[point_keys[idx]] = point_td.getText()
        points.append(point)
    print(points)
    print("-----"*20)
    for grades_tr in grades_trs[1:]:
        grade = {}
        grades_tds = grades_tr.find_all("td")
        for idx, grade_td in enumerate(grades_tds):
            grade[grade_keys[idx]] = grade_td.getText().strip()
        grades.append(grade)
    print(grades)
    infos["统计时间"] = time
    infos["绩点"] = points
    infos["总绩点"] = all_point
    infos["成绩"] = grades
    infos["user_id"] = user_id
    infos["passwd"] = password
    return infos
